package com.ruoyi.eduadmin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.eduadmin.domain.TAttention;
import com.ruoyi.eduadmin.domain.TTeachTrace;
import com.ruoyi.eduadmin.dto.AttentionDto;
import com.ruoyi.eduadmin.mapper.TAttentionMapper;
import com.ruoyi.eduadmin.mapper.TTeachTraceMapper;
import com.ruoyi.eduadmin.service.ITAttentionService;
import com.ruoyi.student.api.StudentService;
import com.ruoyi.student.api.domain.TStudent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 关注列表Service业务层处理
 * 
 * @author hyk
 * @date 2022-12-26
 */
@Service
public class TAttentionServiceImpl implements ITAttentionService 
{
    @Autowired
    private TAttentionMapper tAttentionMapper;

    @Autowired
    private TTeachTraceMapper tTeachTraceMapper;

    @Autowired
    private StudentService studentService;



    /**
     * 查询关注列表
     * 
     * @param id 关注列表主键
     * @return 关注列表
     */
    @Override
    public TAttention selectTAttentionById(Long id)
    {
        return tAttentionMapper.selectTAttentionById(id);
    }

    /**
     * 查询关注列表列表
     * 
     * @param tAttention 关注列表
     * @return 关注列表
     */
    @Override
    public List<TAttention> selectTAttentionList(TAttention tAttention)
    {
//        AjaxResult info = classService.getInfo(2L);
//        System.out.println(info);
        return tAttentionMapper.selectTAttentionList(tAttention);
    }

    /**
     * 新增关注列表
     * 
     * @param tAttention 关注列表
     * @return 结果
     */
    @Override
    public int insertTAttention(TAttention tAttention)
    {
        AjaxResult ajaxResult = studentService.findStudentByName(tAttention.getStudentName());
        if (!ajaxResult.isSuccess()){
            throw new ServiceException("获取失败！");
        }
        Object data = ajaxResult.get("data");
        if (data == null){
            throw new ServiceException("获取失败！");
        }
        TStudent tStudent = JSONObject.parseObject(JSONObject.toJSONString(data), TStudent.class);

        tAttention.setStudentId(tStudent.getId());
        return tAttentionMapper.insertTAttention(tAttention);
    }

    /**
     * 修改关注列表
     * 
     * @param tAttention 关注列表
     * @return 结果
     */
    @Override
    public int updateTAttention(TAttention tAttention)
    {
        return tAttentionMapper.updateTAttention(tAttention);
    }

    /**
     * 批量删除关注列表
     * 
     * @param ids 需要删除的关注列表主键
     * @return 结果
     */
    @Override
    public int deleteTAttentionByIds(Long[] ids)
    {
        return tAttentionMapper.deleteTAttentionByIds(ids);
    }

    /**
     * 删除关注列表信息
     * 
     * @param id 关注列表主键
     * @return 结果
     */
    @Override
    public int deleteTAttentionById(Long id)
    {
        return tAttentionMapper.deleteTAttentionById(id);
    }

    @Override
    public List<TTeachTrace> selectTeacherTrace(Long id) {

        return tAttentionMapper.selectTeacherTrace(id);
    }

    @Override
    public List<AttentionDto> getAttentionByClassName() {

        List<TAttention> attentions = tAttentionMapper.getAttentions();
        List<AttentionDto> tattentions = new ArrayList<>();
        Set<String> classNames = attentions.stream().map(TAttention::getClassName).collect(Collectors.toSet());
        System.out.println(classNames);
        for (String className : classNames) {
            ArrayList<TAttention> tAttentions = new ArrayList<>();
            AttentionDto attentionDto = new AttentionDto();
            for (TAttention attention : attentions) {
                if (className.equals(attention.getClassName())){
                    tAttentions.add(attention);
                }
            }
            attentionDto.setClassName(className);
            attentionDto.setAttentions(tAttentions);
            tattentions.add(attentionDto);
        }
        return tattentions;
    }


}
